<template>
  <div class="cart">
    <h2>购物车</h2>
    <ul>
      <li v-for="(item, index) in commodities" :key="index">
        {{ item.name }} - ¥{{ item.price }} - 数量: {{ item.quantity }}
        <button @click="increaseQuantity(index)">+</button>
        <button @click="decreaseQuantity(index)">-</button>
      </li>
    </ul>
    <p>总价：¥{{ total }}</p>
  </div>
</template>

<script>
export default {
  computed: {
    commodities() {
      return this.$store.state.commodities;
    },
    total() {
      return this.$store.getters.total;
    }
  },
  methods: {
    increaseQuantity(index) {
      this.$store.commit('increaseQuantity', index);
    },
    decreaseQuantity(index) {
      this.$store.commit('decreaseQuantity', index);
    }
  }
}
</script>

<style scoped>
.cart {
  text-align: center;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  margin: 10px 0;
}
button {
  margin: 0 5px;
}
</style>